//
//  NWalletDetailViewController.m
//  News
//
//  Created by zhangtao on 2018/10/10.
//  Copyright © 2018年 zt.td. All rights reserved.
//

#import "NWalletDetailViewController.h"
#import "NWalletDetailHeaderView.h"
#import "NWalletSortCell.h"
#import "NWalletCashDetailCell.h"
#import "WalletFlowModel.h"
#import "YesterdayTopModel.h"

@interface NWalletDetailViewController ()<UITableViewDelegate,UITableViewDataSource>

#define cashIdentifier @"cashIdentifier"
#define sortIdentifier @"sortIdentifier"

@property(nonatomic,strong)UITableView *table;
@property (nonatomic, assign) NSInteger pageIndex;
@property (nonatomic, strong) NSMutableArray *dataArray;
@property(nonatomic,assign)NSInteger type;

@end

@implementation NWalletDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    CGRect tableFrame;
    NSArray *titles;
    NSString *titleName;
    
    self.type = 1;
    
    if (_isBiList) {
        tableFrame =CGRectMake(0, navHeight+40, ACEWidth, ACEHeight-navHeight-40);
        titles = @[@"股币明细",@"现金明细"];
        titleName = @"钱包明细";
    }else{
        tableFrame =CGRectMake(0, navHeight+40, ACEWidth, ACEHeight-navHeight-40-80);
        titles = @[@"昨日",@"累计"];
        titleName = @"金币富豪榜";
        [self getMyTotalTopList];
    }
    
    self.title = titleName;
    
    NWalletDetailHeaderView *top = [[NWalletDetailHeaderView alloc]initWithFrame:CGRectMake(0, navHeight, ACEWidth, 40) items:titles];
    top.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:top];
    
    WEAKSELF;
    top.WalletDetailViewCallBack = ^(NSInteger tag) {
        weakSelf.type = tag + 1;
        [weakSelf refreshAction];
    };
    
    _table = [[UITableView alloc]initWithFrame:tableFrame  style:UITableViewStyleGrouped];
    _table.delegate = self;
    _table.dataSource = self;
    [self.view addSubview:_table];
    [_table registerNib:[UINib nibWithNibName:@"NWalletSortCell" bundle:nil] forCellReuseIdentifier:sortIdentifier];
    [_table registerNib:[UINib nibWithNibName:@"NWalletCashDetailCell" bundle:nil] forCellReuseIdentifier:cashIdentifier];
    [self addRefreshFunction];
    [self addLoadMoreFunction];
}

#pragma mark AddTableViewPlugin
- (void)addRefreshFunction
{
    // 设置回调（一旦进入刷新状态，就调用target的action，也就是调用self的loadNewData方法）
    MJRefreshNormalHeader *header = [MJRefreshNormalHeader
                                     headerWithRefreshingTarget:self
                                     refreshingAction:@selector(refreshAction)];
    // 隐藏时间
    header.lastUpdatedTimeLabel.hidden = YES;
    // 马上进入刷新状态
    [header beginRefreshing];
    // 设置header
    self.table.mj_header = header;
}

- (void)addLoadMoreFunction
{
    MJRefreshAutoStateFooter *footer = [MJRefreshAutoStateFooter
                                        footerWithRefreshingTarget:self
                                        refreshingAction:@selector(loadMoreAction)];
    // 设置footer
    self.table.mj_footer = footer;
    self.table.mj_footer.automaticallyHidden = YES;
}

#pragma mark RefreshAction
- (void)refreshAction
{
    self.pageIndex = 1;
    [self getData:1];
}

- (void)loadMoreAction
{
    self.pageIndex ++;
    [self getData:self.pageIndex];
}

- (void)getData:(NSInteger)pageIndex
{
    if (_isBiList) {
        [[NHTTPManage shareInstance]afGet:HTTP_GET_GetWalletFlow andParam:@{@"token":TOKEN,@"cuid":CUID,@"cp":@(pageIndex),@"paytype":@(self.type)} success:^(id  _Nonnull responseObject) {
            //结束头部刷新
            [self.table.mj_header endRefreshing];
            //结束尾部刷新
            [self.table.mj_footer endRefreshing];
            if (self.dataArray.count > 0 && self.pageIndex == 1) {
                [self.dataArray removeAllObjects];
            }
            [responseObject[@"Data"] enumerateObjectsUsingBlock:^(NSDictionary *obj, NSUInteger idx, BOOL * _Nonnull stop) {
                WalletFlowModel *model = [WalletFlowModel modelObjectWithDictionary:obj];
                [self.dataArray addObject:model];
            }];
            if ([responseObject[@"Data"] count] == 0) {
                [self.table.mj_footer endRefreshingWithNoMoreData];
            }
            [self.table reloadData];
        } failture:^(id  _Nonnull error) {
            //结束头部刷新
            [self.table.mj_header endRefreshing];
            //结束尾部刷新
            [self.table.mj_footer endRefreshing];
            NSLog(@"%@",error);
        }];
    } else {
        if (self.type == 1) {
            [[NHTTPManage shareInstance]afGet:HTTP_GET_GetYesterdayTopList andParam:@{@"token":TOKEN,@"cuid":CUID,@"cp":@(pageIndex)} success:^(id  _Nonnull responseObject) {
                //结束头部刷新
                [self.table.mj_header endRefreshing];
                //结束尾部刷新
                [self.table.mj_footer endRefreshing];
                if (self.dataArray.count > 0 && self.pageIndex == 1) {
                    [self.dataArray removeAllObjects];
                }
                [responseObject[@"Data"] enumerateObjectsUsingBlock:^(NSDictionary *obj, NSUInteger idx, BOOL * _Nonnull stop) {
                    YesterdayTopModel *model = [YesterdayTopModel modelObjectWithDictionary:obj];
                    [self.dataArray addObject:model];
                }];
                if ([responseObject[@"Data"] count] == 0) {
                    [self.table.mj_footer endRefreshingWithNoMoreData];
                }
                [self.table reloadData];
            } failture:^(id  _Nonnull error) {
                //结束头部刷新
                [self.table.mj_header endRefreshing];
                //结束尾部刷新
                [self.table.mj_footer endRefreshing];
                NSLog(@"%@",error);
            }];
        } else {
            [[NHTTPManage shareInstance]afGet:HTTP_GET_GetTotalTopList andParam:@{@"token":TOKEN,@"cuid":CUID,@"cp":@(pageIndex)} success:^(id  _Nonnull responseObject) {
                //结束头部刷新
                [self.table.mj_header endRefreshing];
                //结束尾部刷新
                [self.table.mj_footer endRefreshing];
                if (self.dataArray.count > 0 && self.pageIndex == 1) {
                    [self.dataArray removeAllObjects];
                }
                [responseObject[@"Data"] enumerateObjectsUsingBlock:^(NSDictionary *obj, NSUInteger idx, BOOL * _Nonnull stop) {
                    YesterdayTopModel *model = [YesterdayTopModel modelObjectWithDictionary:obj];
                    [self.dataArray addObject:model];
                }];
                if ([responseObject[@"Data"] count] == 0) {
                    [self.table.mj_footer endRefreshingWithNoMoreData];
                }
                [self.table reloadData];
            } failture:^(id  _Nonnull error) {
                //结束头部刷新
                [self.table.mj_header endRefreshing];
                //结束尾部刷新
                [self.table.mj_footer endRefreshing];
                NSLog(@"%@",error);
            }];
        }
    }
}

- (void)getMyTotalTopList
{
    [[NHTTPManage shareInstance]afGet:HTTP_GET_GetMyTotalTopList andParam:@{@"token":TOKEN,@"cuid":CUID} success:^(id  _Nonnull responseObject) {
        //responseObject[@"Data"]
    } failture:^(id  _Nonnull error) {
        NSLog(@"%@",error);
    }];
}


-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataArray.count;
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return .1;
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    return [UIView new];
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 60;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if (!_isBiList) {
        NWalletSortCell *cell = [tableView dequeueReusableCellWithIdentifier:sortIdentifier];
        cell.leftSortImgV.hidden = NO;
        if (indexPath.row == 0) {
             cell.leftSortImgV.image = [UIImage imageNamed:@"78-78jp"];
        }else if (indexPath.row == 1) {
             cell.leftSortImgV.image = [UIImage imageNamed:@"78-78yp"];
        }else if (indexPath.row == 2) {
            cell.leftSortImgV.image = [UIImage imageNamed:@"78-78tp"];
        }else{
            cell.leftSortImgV.hidden = YES;
        }
        [cell bindData:self.dataArray[indexPath.row]];
        return cell;
    } else  {
        NWalletCashDetailCell *cell = [tableView dequeueReusableCellWithIdentifier:cashIdentifier];
        [cell bindData:self.dataArray[indexPath.row] withType:self.type];
        return cell;
    }
        
}

- (NSMutableArray *)dataArray
{
    if (!_dataArray) {
        _dataArray = [NSMutableArray array];
    }
    return _dataArray;
}

@end
